snapshot: Add functions to append shadows
authorBenjamin Otte <otte@redhat.com>
Thu, 21 Feb 2019 02:26:03 +0000 (03:26 +0100)
committerBenjamin Otte <otte@redhat.com>
Thu, 21 Feb 2019 18:47:28 +0000 (19:47 +0100)
docs/reference/gtk/gtk4-sections.txt
gtk/gtkcssshadowvalue.c
gtk/gtksnapshot.c
gtk/gtksnapshot.h

index bb39c8f935b817746b81c90040ed55d19cdd7730..c82f4b8c43d78906f1820570c487fe7c1e7594bf 100644 (file)
@@ -4412,6 +4412,8 @@ gtk_snapshot_append_layout
 gtk_snapshot_append_linear_gradient
 gtk_snapshot_append_repeating_linear_gradient
 gtk_snapshot_append_border
+gtk_snapshot_append_inset_shadow
+gtk_snapshot_append_outset_shadow
 gtk_snapshot_render_background
 gtk_snapshot_render_frame
 gtk_snapshot_render_focus
index b04c1f58c394efb8765d63db49e19b2ae5e5d361..7e6c41c17ddd2a0438b3cd39f7f46500f9c055aa 100644 (file)
@@ -349,28 +349,19 @@ gtk_css_shadow_value_snapshot_outset (const GtkCssValue    *shadow,
                                       GtkSnapshot          *snapshot,
                                       const GskRoundedRect *border_box)
 {
-  GskRoundedRect outline;
-  GskRenderNode *node;
-  int off_x, off_y;
-
   g_return_if_fail (shadow->class == &GTK_CSS_VALUE_SHADOW);
 
   /* We don't need to draw invisible shadows */
   if (gdk_rgba_is_clear (_gtk_css_rgba_value_get_rgba (shadow->color)))
     return;
 
-  gtk_snapshot_get_offset (snapshot, &off_x, &off_y);
-  gsk_rounded_rect_init_copy (&outline, border_box);
-  gsk_rounded_rect_offset (&outline, off_x, off_y);
-
-  node = gsk_outset_shadow_node_new (&outline, 
+  gtk_snapshot_append_outset_shadow (snapshot,
+                                     border_box,
                                      _gtk_css_rgba_value_get_rgba (shadow->color),
                                      _gtk_css_number_value_get (shadow->hoffset, 0),
                                      _gtk_css_number_value_get (shadow->voffset, 0),
                                      _gtk_css_number_value_get (shadow->spread, 0),
                                      _gtk_css_number_value_get (shadow->radius, 0));
-  gtk_snapshot_append_node_internal (snapshot, node);
-  gsk_render_node_unref (node);
 }
 
 void
@@ -378,28 +369,19 @@ gtk_css_shadow_value_snapshot_inset (const GtkCssValue   *shadow,
                                      GtkSnapshot         *snapshot,
                                      const GskRoundedRect*padding_box)
 {
-  GskRoundedRect outline;
-  GskRenderNode *node;
-  int off_x, off_y;
-
   g_return_if_fail (shadow->class == &GTK_CSS_VALUE_SHADOW);
 
   /* We don't need to draw invisible shadows */
   if (gdk_rgba_is_clear (_gtk_css_rgba_value_get_rgba (shadow->color)))
     return;
 
-  gtk_snapshot_get_offset (snapshot, &off_x, &off_y);
-  gsk_rounded_rect_init_copy (&outline, padding_box);
-  gsk_rounded_rect_offset (&outline, off_x, off_y);
-
-  node = gsk_inset_shadow_node_new (&outline, 
+  gtk_snapshot_append_inset_shadow (snapshot,
+                                    padding_box, 
                                     _gtk_css_rgba_value_get_rgba (shadow->color),
                                     _gtk_css_number_value_get (shadow->hoffset, 0),
                                     _gtk_css_number_value_get (shadow->voffset, 0),
                                     _gtk_css_number_value_get (shadow->spread, 0),
                                     _gtk_css_number_value_get (shadow->radius, 0));
-  gtk_snapshot_append_node_internal (snapshot, node);
-  gsk_render_node_unref (node);
 }
 
 gboolean
index eb666dc5ad5cbdfc0a59a7021fef0b4b93c70c9d..0caa0ef07897a6b0e0161a7dc9fbf5fd0b8f1dea 100644 (file)
@@ -1636,3 +1636,91 @@ gtk_snapshot_append_border (GtkSnapshot          *snapshot,
   gtk_snapshot_append_node_internal (snapshot, node);
   gsk_render_node_unref (node);
 }
+
+/**
+ * gtk_snapshot_append_inset_shadow:
+ * @snapshot: a #GtkSnapshot
+ * @outline: outline of the region surrounded by shadow
+ * @color: color of the shadow
+ * @dx: horizontal offset of shadow
+ * @dy: vertical offset of shadow
+ * @spread: how far the shadow spreads towards the inside
+ * @blur_radius: how much blur to apply to the shadow
+ *
+ * Appends an inset shadow into the box given by @outline.
+ */
+void
+gtk_snapshot_append_inset_shadow (GtkSnapshot          *snapshot,
+                                  const GskRoundedRect *outline,
+                                  const GdkRGBA        *color,
+                                  float                 dx,
+                                  float                 dy,
+                                  float                 spread,
+                                  float                 blur_radius)
+{
+  GskRenderNode *node;
+  GskRoundedRect real_outline;
+  float scale_x, scale_y, x, y;
+
+  g_return_if_fail (snapshot != NULL);
+  g_return_if_fail (outline != NULL);
+  g_return_if_fail (color != NULL);
+
+  gtk_snapshot_ensure_affine (snapshot, &scale_x, &scale_y, &x, &y);
+  gtk_rounded_rect_scale_affine (&real_outline, outline, scale_x, scale_y, x, y);
+
+  node = gsk_inset_shadow_node_new (&real_outline,
+                                    color,
+                                    scale_x * dx + x,
+                                    scale_y * dy + y,
+                                    spread,
+                                    blur_radius);
+
+  gtk_snapshot_append_node_internal (snapshot, node);
+  gsk_render_node_unref (node);
+}
+
+/**
+ * gtk_snapshot_append_outset_shadow:
+ * @snapshot: a #GtkSnapshot
+ * @outline: outline of the region surrounded by shadow
+ * @color: color of the shadow
+ * @dx: horizontal offset of shadow
+ * @dy: vertical offset of shadow
+ * @spread: how far the shadow spreads towards the outside
+ * @blur_radius: how much blur to apply to the shadow
+ *
+ * Appends an outset shadow node around the box given by @outline.
+ */
+void
+gtk_snapshot_append_outset_shadow (GtkSnapshot          *snapshot,
+                                   const GskRoundedRect *outline,
+                                   const GdkRGBA        *color,
+                                   float                 dx,
+                                   float                 dy,
+                                   float                 spread,
+                                   float                 blur_radius)
+{
+  GskRenderNode *node;
+  GskRoundedRect real_outline;
+  float scale_x, scale_y, x, y;
+
+  g_return_if_fail (snapshot != NULL);
+  g_return_if_fail (outline != NULL);
+  g_return_if_fail (color != NULL);
+
+  gtk_snapshot_ensure_affine (snapshot, &scale_x, &scale_y, &x, &y);
+  gtk_rounded_rect_scale_affine (&real_outline, outline, scale_x, scale_y, x, y);
+
+  node = gsk_outset_shadow_node_new (&real_outline,
+                                     color,
+                                     scale_x * dx + x,
+                                     scale_y * dy + y,
+                                     spread,
+                                     blur_radius);
+
+
+  gtk_snapshot_append_node_internal (snapshot, node);
+  gsk_render_node_unref (node);
+}
+
index 1f61d7a214d02862dc0b0354ec37f6ca99888e34..05cf3f586726d8712217d6a70d3374c50e9969b8 100644 (file)
@@ -146,6 +146,22 @@ void            gtk_snapshot_append_border              (GtkSnapshot
                                                          const GskRoundedRect   *outline,
                                                          const float             border_width[4],
                                                          const GdkRGBA           border_color[4]);
+GDK_AVAILABLE_IN_ALL
+void            gtk_snapshot_append_inset_shadow        (GtkSnapshot            *snapshot,
+                                                         const GskRoundedRect   *outline,
+                                                         const GdkRGBA          *color,
+                                                         float                   dx,
+                                                         float                   dy,
+                                                         float                   spread,
+                                                         float                   blur_radius);
+GDK_AVAILABLE_IN_ALL
+void            gtk_snapshot_append_outset_shadow       (GtkSnapshot            *snapshot,
+                                                         const GskRoundedRect   *outline,
+                                                         const GdkRGBA          *color,
+                                                         float                   dx,
+                                                         float                   dy,
+                                                         float                   spread,
+                                                         float                   blur_radius);
 /* next function implemented in gskpango.c */
 GDK_AVAILABLE_IN_ALL
 void            gtk_snapshot_append_layout              (GtkSnapshot            *snapshot,